#include "log.h"
#include "uart.h"

#define SUPPORT_ADV 0

bool m_logEnalne = TRUE;
bool m_logInitState = TRUE;

#if DEBUG_LOG_EN == 0

void LogSetEnable(bool en)
{
    
}

void LogString(uint8_t *str)
{

}

void Log(const char *fmt, ...)
{

}

bool LogGetEnable(void)
{
    return FALSE;
}

bool LogGetInitEnable(void)
{
    return FALSE;
}

#else 

#define ISDIGIT(c)  ((c - '0') >= 0 && (c - '0') <= 9)

static void printn(unsigned int n, unsigned int b)
{
    static char *ntab = "0123456789ABCDEF";
    unsigned int a, m;
    
    if (n / b)
    {
        a = n / b;
        printn(a, b);  
    }
    m = n % b;
    UartSendByte(LOG_UART_NO, ntab[m]);
}

static void printfloat(float n, int dp)
{
#if SUPPORT_ADV > 0
    int v1 = (int)n;
    int p = 1;
    int v2;
    uint8_t i;

    n += 0.0000001;
    dp += 1;
    for(i = 0; i < dp; i++)
    {
        p *= 10;
    }
    v2 = (int)((n - v1) * p);
    v2 /= 10;

    printn(v1, 10);
    UartSendString(LOG_UART_NO, ".");
    printn(v2, 10);
#endif
}

void LogSetEnable(bool en)
{
    static bool isInit = FALSE;

    if(!isInit)
    {
        isInit = TRUE;
        m_logInitState = en;
    }
    
    m_logEnalne = en;
}

bool LogGetEnable(void)
{
    return m_logEnalne;
}

bool LogGetInitEnable(void)
{
    return m_logInitState;
}

void LogString(uint8_t *str)
{
    if(m_logEnalne == FALSE)
        return;

    UartSendString(LOG_UART_NO, str);
}

void Log(const char *fmt, ...)
{
    char c;
    unsigned int *adx = (unsigned int*)(void*)&fmt + 1;

    if(m_logEnalne == FALSE)
        return;
        
_loop:
    while((c = *fmt++) != '%')
    {
        if (c == '\0') return;
        UartSendByte(LOG_UART_NO, c);
    }

    c = *fmt++;
    if (c == 'd')
    {
        printn(*adx, 10);
        adx += sizeof(int*);
    }
    else if (c == 's')
    {
        UartSendString(LOG_UART_NO, (char*)*adx);
        adx += sizeof(char*);
    }
    else if (c == 'o' || c == 'x')
    {
        printn(*adx, c=='o'? 8:16);
        adx += sizeof(int*);
    }
#if SUPPORT_ADV > 0
    else if(c == 'l')
    {
        if(*(fmt + 1) != '\0')
        {
            char next = *fmt++;
            if(next == 'd')
            {
                static uint8_t ld_buf[11] = {0};  
                int32_t lv = *((int32_t*)adx);
                uint8_t len = 0;
                int8_t i;

                if(lv < 0)
                {
                    UartSendByte(LOG_UART_NO, '-');
                    lv = -lv;
                }
                while (TRUE)
                {
                    ld_buf[len] = lv % 10 + '0';
                    len ++;
                    lv /= 10;
                    if(lv == 0)
                        break;
                }
                
                if(len > 0)
                {
                    for(i = len - 1; i >= 0; i --)
                    {
                        UartSendByte(LOG_UART_NO, ld_buf[i]);
                    }
                }
                adx += sizeof(uint32_t);
            }
        }
    }
    else if(c == 'f')
    {
        printfloat(*((float*)adx), 3);
        adx += sizeof(float *);
    }
    else if(ISDIGIT(c) && *(fmt + 1) != '\0')
    {
        char next = *fmt++;
        if(next == 'f')
        {
            printfloat(*((float*)adx), (int)(c - '0'));
            adx += sizeof(float *);
        }
    }
#endif
    
    goto _loop;
} 

#endif 